package com.cyys.modules.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class DateMetaObjectHandler implements MetaObjectHandler {
    /**
     * 创建时间
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.hasGetter("dateCreate")) {
            setFieldValByName("dateCreate", new Date(), metaObject);
        }

        if (metaObject.hasGetter("dateUpdate")){
            setFieldValByName("dateUpdate", new Date(), metaObject);
        }
    }

    /**
     * 最后一次更新时间
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        //setUpdateFieldValByName("dateUpdate", new Date(), metaObject);
        if (metaObject.hasGetter("dateUpdate")) {
            setFieldValByName("dateUpdate", new Date(), metaObject);
        }
    }
}
